package club.xyes.zkh.ecommerce.web.api.controller.test;

import club.xyes.zkh.ecommerce.commons.properties.WechatConfigurationProperties;
import club.xyes.zkh.ecommerce.commons.vo.GeneralResult;
import club.xyes.zkh.ecommerce.service.UserService;
import club.xyes.zkh.ecommerce.wechat.dto.WxAccessToken;
import club.xyes.zkh.ecommerce.wechat.dto.WxOauth2AccessTokenResult;
import club.xyes.zkh.ecommerce.wechat.service.WeChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * Create by 郭文梁 2019/4/29 0029 17:51
 * WxApiTestController
 * 微信Api测试控制器
 *
 * @author 郭文梁
 * @data 2019/4/29 0029
 */
@RestController
@RequestMapping("/api/test/wx")
public class WxApiTestController {
    private final WechatConfigurationProperties wechatConfigurationProperties;
    private final WeChatService weChatService;
    private final UserService userService;

    @Autowired
    public WxApiTestController(WechatConfigurationProperties wechatConfigurationProperties, WeChatService weChatService, UserService userService) {
        this.wechatConfigurationProperties = wechatConfigurationProperties;
        this.weChatService = weChatService;
        this.userService = userService;
    }

    @GetMapping("/token")
    public GeneralResult<?> getToken() throws IOException {
        String appId = wechatConfigurationProperties.getUserMicroProgramAppId();
        String secret = wechatConfigurationProperties.getUserMicroProgramSecret();
        WxAccessToken token = weChatService.getAccessToken(appId, secret);
        return GeneralResult.ok(token);
    }


    @GetMapping("/code")
    public GeneralResult<?> getCode(String code) throws IOException {
        String appId = wechatConfigurationProperties.getServiceAccountAppId();
        String secret = wechatConfigurationProperties.getServiceAccountSecret();
        WxOauth2AccessTokenResult result = weChatService.oauth2CodeToken(appId, secret, code);
        return GeneralResult.ok(result);
    }
}
